
package kalkon;

import java.io.IOException;
import java.io.InputStream;
import java.util.ResourceBundle;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.ParsingException;
import nu.xom.Text;
import nu.xom.xslt.XSLException;
import nu.xom.xslt.XSLTransform;

/**
 *
 * @author tommz
 */
public class Transformer {

    private XSLTransform transform = null;

    /**
     * Factory method for Transformes.
     *
     * @param transformation open inputStream with XSLT.
     * @return Transformer or null if some error occured.
     */
    public static Transformer getTransformer(InputStream transformation){

        ResourceBundle bundle = ResourceBundle.getBundle("kalkon.messages");

        XSLTransform transform = null;

        try {
            Builder builder = new Builder();
            Document stylesheet = builder.build(transformation);
            transform = new XSLTransform(stylesheet);
        } catch (XSLException ex) {
            System.err.println(bundle.getString("CL.reportBug"));
            System.err.println("XSLT error");
        } catch (ParsingException ex) {
            System.err.println(bundle.getString("CL.reportBug"));
            System.err.println("Well-formedness error in " + ex.getURI());
        } catch (IOException ex) {
            System.err.println(bundle.getString("CL.reportBug"));
            System.err.println("I/O error while reading input document or stylesheet");
        }

        if(transform == null){
            return null;
        }

        return new Transformer(transform);
    }

    private Transformer(XSLTransform transform){
        this.transform = transform;
    }

    /**
     * Applies transformation to input document.
     * @param inputDoc
     * @return the first node of result.
     */
    public Node transform(Document inputDoc){
        Node node = null;
        try {
            node = transform.transform(inputDoc).get(0);
        } catch (XSLException ex) {
            System.err.println(ResourceBundle.getBundle("kalkon.messages")
                    .getString("CL.reportBug"));
            ex.printStackTrace();
        }

        return node;
    }

    /**
     * Applies transformation to input document.
     *
     * See public Node transform(Document inputDoc)
     *
     * @param inputDoc
     * @return
     */
    public Document transformToDocument(Document inputDoc){
        Document doc = new Document((Element) transform(inputDoc));
        return doc;
    }

    /**
     * Applies transformation to input document.
     *
     * See public Node transform(Document inputDoc)
     *
     * @param inputDoc
     * @return
     */
    public Text transformToText(Document inputDoc){
        return (Text) transform(inputDoc);
    }
    /**
     * Sets parameter to the transformer
     * @param name name of the parameter
     * @param value value of the parameter
     */
    public void setParameter(String name, String value){
        transform.setParameter(name, value);
    }

}
